// pages/visitors/tools/posterCard/posterCard.js
const app = getApp();
import api from "../../../../common/api";
import util from "../../../../utils/util";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canvasWidth: 0,
    canvasHeight: 0,
    selectTmpID: '',
    paintPallette: {},
    posterData: {},
    isHidePage: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let t = this;
    t.initSystemInfo()
    t.initData(options)
    t.loadPosterData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  initData(options) {
    let t = this
    if (options.title) {
      wx.setNavigationBarTitle({
        title: options.title
      })
    }

    t.myData = {
      currentClickBtn: '',
      posterTmp: app.globalData.posterTmp || {},
    }

    t.setData({
      selectTmpID: t.myData.posterTmp.tmpID || '',
    })

    t.myData.currentSubCom = t.selectComponent(`.${t.myData.posterTmp.tmpID}`)
  },

  initSystemInfo() {
    let t = this
    wx.getSystemInfo({
      success: function (res) {
        t.setData({
          canvasWidth: res.windowWidth,
          canvasHeight: res.windowHeight
        })
      }
    })
  },

  onDrawSuccess(e) {
    let t = this
    wx.hideLoading()
    let img = e.detail.path
    if (img && typeof img === 'string') {
      if (t.myData.currentClickBtn === 'save') {
        wx.saveImageToPhotosAlbum({
          filePath: img,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
            })
          },
          fail: () => {
            wx.getSetting({
              success: (res) => {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                  wx.showModal({
                    title: '提示',
                    content: '需要获取图片权限哦',
                    success: (res) => {
                      if (res.confirm) {
                        wx.openSetting()
                      }
                    }
                  })
                }
              }
            })
          }
        });
      }
      if (t.myData.currentClickBtn === 'share') {
        wx.previewImage({
          current: img, // 当前显示图片的http链接
          urls: [img], // 需要预览的图片http链接列表
        }, true)
      }
    } else {
      wx.showToast({
        title: '海报制作失败，请稍后再试！',
        icon: 'none',
      })
    }
  },

  onDrawFail() {
    wx.hideLoading()
    wx.showToast({
      title: '海报制作失败，请稍后再试！',
      icon: 'none',
    })
  },

  onSaveImg() {
    let t = this
    wx.showLoading({
      mask: true,
      title: '海报制作中...',
    })
    t.myData.currentClickBtn = 'save'
    t.startDrawPoster()
  },

  onShareImg() {
    let t = this
    wx.showLoading({
      mask: true,
      title: '海报制作中...',
    })
    t.myData.currentClickBtn = 'share'
    t.startDrawPoster()
  },

  startDrawPoster() {
    let t = this
    t.myData.currentSubCom.transmitPosterConfig()
    t.setData({
      paintPallette: t.posterConfig || {}
    })
  },

  getPosterConfig(e) {
    let t = this
    t.posterConfig = e.detail
  },

  loadPosterData() {
    let t = this;
    return new Promise((resolve, reject) => {
      let loginUserInfo = getApp().globalData.loginUserInfo;
      let settingInfo = wx.getStorageSync('settingInfo');
      let jingjiId = {
        agentId: loginUserInfo.data.content.sessionUser.id
      }
      api.qrcodeLongToShort(JSON.stringify(jingjiId), {}, res => {
        let smallCode = res.data.content;
        let QrCode = util.domain + "miniCode/getWXACodeUnlimit?page=" + encodeURI("pages/houseShare/personalShop/personalShop") + "&scene=" + smallCode + "&miniName=zhaofangjs";
        t.setData({
          isHidePage: false,
          posterData: {
            qrCode: QrCode || '',
            bgImg: t.myData.posterTmp.bgImg || '',
            userAddress: settingInfo.address ? settingInfo.address : loginUserInfo.data.content.sessionUser.extProps.storeAdress,
            userAvatar: settingInfo.imgArray ? settingInfo.imgArray : loginUserInfo.data.content.sessionUser.avatar,
            userName: settingInfo.name ? settingInfo.name : loginUserInfo.data.content.sessionUser.realName,
            userBrand: settingInfo.storeName ? settingInfo.storeName : loginUserInfo.data.content.sessionUser.extProps.storeName,
            userMobile: settingInfo.tel ? settingInfo.tel : loginUserInfo.data.content.sessionUser.mobile
          }
        })
        resolve()
      })
    })
  },

})